# _*_ encoding: utf-8 _*_
# B站UP粉丝和关注数据爬虫
import requests

class FansSpider(object):
    api = "https://api.bilibili.com/x/relation/stat"
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"
    }
    def __init__(self, vmid):
        '''
        vmid: UP主的ID
        '''
        self.vmid = vmid
        # 粉丝数量
        self.fans = None
        # UP关注人数
        self.follows = None

    def getFansAndfollows(self) -> object:
        '''发起请求'''
        params = {
            "vmid": self.vmid,
            "jsonp": "jsonp"
        }
        response = requests.get(
            self.api, params=params, headers=self.headers
        )
        data = response.json().get("data")
        # 记录
        self.fans = data.get("follower")
        self.follows = data.get("following")
        return self

    def print(self) -> None:
        '''打印数据'''
        if self.fans is not None and self.follows is not None:
            print(f"UP：{self.vmid} - <粉丝数量：{self.fans:,}; 关注人数: {self.follows}>")
        return

if __name__ == "__main__":
    FansSpider("163637592")
